package org.example.gui.panel;

import org.example.dao.CategoryDAO;
import org.example.entity.Category;
import org.example.gui.listener.RecordListener;
import org.example.gui.model.CategoryComboBoxModel;
import org.example.util.ColorUtil;
import org.example.util.GUIUtil;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

/**
 * 界面类 RecordPanel 记一笔页面
 * GridLayout，网格布局
 */

public class RecordPanel extends WorkingPanel {
    public static RecordPanel instance = new RecordPanel();
    public int updateId = -1; //如果changeId>0则为修改模式，<0为模式的添加模式

    public JTextField tfSpend = new JTextField("0");

    public CategoryComboBoxModel cbModel = new CategoryComboBoxModel();
    public JComboBox<Category> cbCategory = new JComboBox<>(cbModel);
    public JTextField tfComment = new JTextField();
    public JTextField tfLocation = new JTextField();
    public JXDatePicker datePick = new JXDatePicker(new Date());

    private final JButton bSubmit = new JButton("保存");


    public RecordPanel() {
        JLabel lSpend = new JLabel("花费(￥)");
        JLabel lCategory = new JLabel("分类");
        JLabel lComment = new JLabel("备注");
        JLabel lLocation = new JLabel("地点");
        JLabel lDate = new JLabel("日期");

        GUIUtil.setColor(ColorUtil.grayColor, lSpend, lCategory, lComment, lLocation, lDate);
        GUIUtil.setColor(ColorUtil.blueColor, bSubmit);
        JPanel pInput = new JPanel();
        JPanel pSubmit = new JPanel();
        pInput.setLayout(new GridLayout(10, 1));

        pInput.add(lSpend);
        pInput.add(tfSpend);
        pInput.add(lCategory);
        pInput.add(cbCategory);
        pInput.add(lComment);
        pInput.add(tfComment);
        pInput.add(lLocation);
        pInput.add(tfLocation);
        pInput.add(lDate);
        pInput.add(datePick);

        pSubmit.add(bSubmit);

        this.setLayout(new BorderLayout());
        this.add(pInput, BorderLayout.NORTH);
        this.add(pSubmit, BorderLayout.CENTER);

        addListener();
    }



    @Override
    public void updateData() {
        cbModel.categoryList = CategoryDAO.listAllCategory();
        cbCategory.updateUI();
        tfSpend.setText("");
        tfComment.setText("");
        tfLocation.setText("");
        datePick.setDate(new Date());
        tfSpend.grabFocus();
    }


    public Category getSelectedCategory() {
        return (Category) cbCategory.getSelectedItem();
    }


    @Override
    public void addListener() {
        bSubmit.addActionListener(new RecordListener());
    }
}